import service, { AxiosRequestConfig } from './axios'
export * from './types'

export const request = <T = any>(config: AxiosRequestConfig): Promise<T> => {
	// 创建一个Promise对象
	return new Promise((resolve, reject) => {
		// 调用service的request方法，传入config参数
		service
			.request(config)
			.then((res) => {
				// 一些业务处理
				resolve(res.data)
			})
			.catch((err) => {
				console.log('request error' + err)
				reject(err)
			})
	})
}
const http = {
	get<T = any>(
		url: string,
		params = {},
		config?: AxiosRequestConfig
	): Promise<T> {
		return request({ url, params,...config, method: 'GET' })
	},
	post<T = any>(
		url: string,
		data = {},
		config?: AxiosRequestConfig
	): Promise<T> {
		return request({ url, data,...config, method: 'POST' })
	},
	put<T = any>(
		url: string,
		data = {},
		config?: AxiosRequestConfig
	): Promise<T> {
		return request({ url, data,...config, method: 'PUT' })
	},
	delete<T = any>(
		url: string,
		data = {},
		config?: AxiosRequestConfig
	): Promise<T> {
		return request({ url, data,...config, method: 'DELETE' })
	},
	// 上传文件，指定 'Content-Type':'multipart/form-data'
	upload<T = any>(
		url: string,
		data = {},
		config?: AxiosRequestConfig
	): Promise<T> {
		return request({
			url,
			data,
			...config,
			method: 'POST',
			headers: { 'Content-Type':'multipart/form-data' },
		})
	},
}

export default http
